package com.dhh.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CUtil {	
	public static NumberFormat nf =NumberFormat.getNumberInstance(); 
	public static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat format = new SimpleDateFormat();
	
	static{
		nf.setMaximumFractionDigits(1); 
	}
	
	public static String formatNum(Number v){
		return nf.format(v);
	}
	
	public static String getCup(double quatity){
		return nf.format(quatity/228);
	}
	
	public static String getOunce(double quatity){
		return nf.format(quatity/28.3495231);
	}
	
	public static String getQuatityFromCup(double cup){
		return nf.format(cup * 228);
	}
	
	public static String getQuatityFromOunce(double oz){
		return nf.format(oz * 28.3495231);
	}
	
	public static String getDate(Date date){
		return sf.format(date);
	}
	
	public static String getDate(String pattern, Date date){
		sf.applyPattern(pattern);
		return sf.format(date);
	}
	
	
	public static String getDate(String f1,String f2,String date){
		format.applyPattern(f1);
		try {
			Date d = format.parse(date);
			format.applyPattern(f2);
			return format.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getToday(){
		return sf.format(Calendar.getInstance().getTime());
	}
	
	public static Date parseDate(String pattern, String date){
		SimpleDateFormat sf = null;
		Date rd = null;
			try {
				if(pattern==null){
					sf = new SimpleDateFormat("yyyy-MM-dd");
				}else{
					sf = new SimpleDateFormat(pattern);
				}
				
				rd = sf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		
		return rd;
		
	}
	
	public static int convertToCM(String inches){
		String[] is = inches.split("'");		
		int feet = Integer.parseInt(is[0]);
		int inch = Integer.parseInt(is[1]);
		
		float s1 = feet * 30.48f;
		float s2 = inch * 2.54f;		
		int sum = Math.round(s1+s2);
		
		return sum;
	}
	
	public static double convertToKG(double lbs){
		double kg = lbs * 0.45359237;
		return Double.valueOf(nf.format(kg));
	}
	
}
